package edu.nf.waimai.dao.user.impl;


import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.user.ShoppingDAO;
import edu.nf.waimai.model.user.ShoppingCart;
import edu.nf.waimai.model.user.UserShopPingCart;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023/6/19
 * @Author zgm
 */
public class ShopPingDAOImpl implements ShoppingDAO {
    @Override
    public int addShopPing(ShoppingCart shopping) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "INSERT INTO shopping_cart(u_id,item_id,shop_id,item_num,shopping_cart_status,shopping_cart_details) VALUES(?,?,?,?,0,?)";
        return executor.executeUpdate(sql,
                shopping.getUid(),
                shopping.getItemId(),
                shopping.getShopId(),
                shopping.getItemNum(),
                shopping.getShoppingCartDetails());
    }

    @Override
    public List<UserShopPingCart> listShopping(int userId, int shopId) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT shopping_cart.shopping_cart_id, shopping_cart.u_id, shopping_cart.item_num,items.item_picture, items.item_id, items.item_name, items.item_price "+
                ",shopping_cart.shopping_cart_details FROM shopping_cart INNER JOIN items ON shopping_cart.item_id = items.item_id "+
                " where shopping_cart.u_id = ? and shopping_cart.shop_id = ? and shopping_cart_status = 0";
        return executor.executeQuery(sql,
                new BeanListHandler<>(UserShopPingCart.class),
                userId,shopId);
    }

    @Override
    public int updateShopPing(int userId, int shopId) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE shopping_cart SET shopping_cart_status = 1 WHERE u_id = ? AND shop_id = ?;";
        return executor.executeUpdate(sql,userId,shopId);
    }

    @Override
    public int updateShopPingItemNum(int id, int itemNum) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE shopping_cart SET item_num = ? WHERE shopping_cart_id = ?;";
        return executor.executeUpdate(sql,itemNum,id);
    }

    @Override
    public int deleteShopPing(int id) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "DELETE FROM shopping_cart WHERE shopping_cart_id = ?;";
        return executor.executeUpdate(sql,id);
    }
}
